package com.youhome.service.service.impl;


import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.mapper.TruliaDataMapper;
import com.youhome.service.model.TruliaData;
import com.youhome.service.model.TruliaDataWithBLOBs;
import com.youhome.service.response.CommuteIndex;
import com.youhome.service.service.TruliaDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by chen365 on 2017/9/11.
 */
@Service
public class TruliaDataServiceImpl implements TruliaDataService {
    @Autowired
    private TruliaDataMapper truliaDataMapper;

    @Override
    public TruliaDataWithBLOBs selectByZipcode(String zipcode) throws Exception {
        return truliaDataMapper.selectByZipcode(zipcode);
    }

    @Override
    public CommuteIndex selectCommuteIndexByZip(String zip) throws CustomException {
        TruliaData truliaData = truliaDataMapper.selectByZipcode(zip);
        CommuteIndex commuteIndex = new CommuteIndex();
        if (truliaData == null) {
            throw new CustomException(ResultCode.NO_DATA_WAS_FOUND);
        }
        //骑车通勤比例%
        commuteIndex.setBike(truliaData.getCommuteBike());
        //驾车通勤比例%
        commuteIndex.setCar(truliaData.getCommuteCar());
        //步行通勤比例%
        commuteIndex.setFoot(truliaData.getCommuteFoot());
        //公交通勤比例%
        commuteIndex.setTransit(truliaData.getCommuteTransit());
        return commuteIndex;
    }
}
